<?php

class Medal_reception_model extends CI_Model {

    function MedalReceptionModel() {
        parent::Model();
    }

    function totalReceipt() {
        $allAmount = $this->_getAllAmount();
        return intval($allAmount[0]['possible_amount']);
    }

    function _getAllAmount() {
        $this->db->select_sum("possible_amount");
        $this->db->where(" Medal_ID ='" . check_post("medalofficer") . "' AND Official_Order_ID = '" . check_post("official_order_ID") . "'");
        $query = $this->db->get('Official_Medal_Order');
        return $query->result_array();
    }

    function ReceiptTaken() {
        $this->db->select("ID");
        $this->db->where(" medal_ID ='" . check_post("medalofficer") . "' AND official_order_ID = '" . check_post("official_order_ID") . "'");
        $query = $this->db->get('Medal_Reception');
        return intval($query->num_rows());
    }

    function add() {
        $query = $this->db->get_where('Medal_Reception', array('medal_ID' => check_post('medalofficer'), 'official_order_ID' => check_post('official_order_ID'), 'officer_ID' => check_post('officialname')));
        if (!$query->num_rows) {
            $this->db->insert('Medal_Reception', array(
                'officer_ID' => check_post('officialname'),
                'medal_ID' => check_post('medalofficer'),
                'official_order_ID' => check_post('official_order_ID'),
                '`date`' => $this->db->convertDate(check_post("receptiondate"))
            ));
            // combine_session('success', 'Added new officer successfully.');
            $return['result'] = true;
            $return['message'] = 'បន្ថែមមន្ត្រីទទួលមេដាយបានជោគជ័យ';
        }else {
            $return['result'] = false;
            $return['message'] = 'មន្ត្រីនេះធ្លាប់ទទួលមេដាយនេះរួចហើយ.';
        }
        return $return;
    }

    function get_medal_reception() {
        $aColumns = array('Medal_Reception.ID', 'Officer_Function.name', 'Officer.name', 'Medal.full_name', 'Medal_Reception.`date`');

        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Medal_Reception.ID", "ASC");
        }
        $order = $this->db->ar_orderby;

        $where = " Medal_Reception.official_order_ID = '" . check_get('official_order_ID') . "' ";

        $this->db->where($where);

        $this->db->select('Medal_Reception.ID');
        $this->db->join('Medal', 'Medal_Reception.medal_ID= Medal.ID', 'inner');
        $this->db->join('Officer', 'Medal_Reception.officer_ID= Officer.ID', 'inner');
        $this->db->join('Officer_Function', 'Officer.function_ID= Officer_Function.ID', 'inner');
        $totalQuery = $this->db->get('Medal_Reception');

        $this->db->where($where);
        $this->db->select('Medal_Reception.ID, Officer_Function.name functionName, Officer.name officerName, Medal.full_name medalName, Medal_Reception.`date` `date`');
        $this->db->join('Medal', 'Medal_Reception.medal_ID= Medal.ID', 'inner');
        $this->db->join('Officer', 'Medal_Reception.officer_ID= Officer.ID', 'inner');
        $this->db->join('Officer_Function', 'Officer.function_ID= Officer_Function.ID', 'inner');
        $query = $this->db->get('Medal_Reception', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $medal_receptions = $query->result_array();
        $aaData = array();
        $i = 0;
        foreach ($medal_receptions as $medal_reception) {
            $data = array(++$i, $medal_reception['functionName'], $medal_reception['officerName'], $medal_reception['medalName'], date("d/m/Y", strtotime($medal_reception['date'])), "<a class='delete-btn delete-medal-reception' rel='{$medal_reception['ID']}' title='លុប'>&nbsp;</a>");
            array_push($aaData, $data);
        }


        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }

    
    function modelReceiptDatatable(){        
        $aColumns = array('Medal_Reception.ID', 'Medal_Type.name', 'Medal_Class.name', 'Official_Order.Order_Number', 'Medal_Reception.`date`');

        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Medal_Reception.ID", "ASC");
        }
        $order = $this->db->ar_orderby;

        $where = " Medal_Reception.officer_ID = '" . check_get('officer_ID') . "' ";

        $this->db->where($where);

        $this->db->select('Medal_Reception.ID');
        $this->db->join('Medal', 'Medal_Reception.medal_ID= Medal.ID', 'inner');
        $this->db->join('Officer', 'Medal_Reception.officer_ID= Officer.ID', 'inner');
        $this->db->join('Official_Order', 'Official_Order.ID= Medal_Reception.official_order_ID', 'inner');
        $this->db->join('Medal_Type', 'Medal.Medal_Type_ID= Medal_Type.ID', 'inner');
        $this->db->join('Medal_Class', 'Medal.Medal_Class_ID= Medal_Class.ID', 'inner');
        $totalQuery = $this->db->get('Medal_Reception');

        $this->db->where($where);
        $this->db->select('Medal_Reception.ID, Medal_Type.name typeName, Medal_Class.name className, Official_Order.Order_Number, Medal_Reception.`date`');
        $this->db->join('Medal', 'Medal_Reception.medal_ID= Medal.ID', 'inner');
        $this->db->join('Officer', 'Medal_Reception.officer_ID= Officer.ID', 'inner');
        $this->db->join('Official_Order', 'Official_Order.ID= Medal_Reception.official_order_ID', 'inner');
        $this->db->join('Medal_Type', 'Medal.Medal_Type_ID= Medal_Type.ID', 'inner');
        $this->db->join('Medal_Class', 'Medal.Medal_Class_ID= Medal_Class.ID', 'inner');
        $query = $this->db->get('Medal_Reception', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $medal_receptions = $query->result_array();
        $aaData = array();
        $i = 0;
        foreach ($medal_receptions as $medal_reception) {
            $data = array(++$i, $medal_reception['typeName'], $medal_reception['className'], $medal_reception['Order_Number'], date("d/m/Y", strtotime($medal_reception['date'])), "<a class='delete-btn delete-medal-reception' rel='{$medal_reception['ID']}' title='លុប'>&nbsp;</a>");
            array_push($aaData, $data);
        }


        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }
    
    function deleteMedalReception() {
        $this->db->delete('Medal_Reception', array('ID' => check_post("ID")));
    }

}

?>